package com.gzu.shop.config;

import com.gzu.shop.interceptor.JwtTokenUserInterceptor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author Clown
 */
@Configuration
@EnableWebMvc
@Slf4j
@AllArgsConstructor
public class WebConfig implements WebMvcConfigurer {
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    // http://localhost:8085/doc.html#/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/error")
                .excludePathPatterns("/v3/api-docs/**")
                .excludePathPatterns("/swagger-ui/index.html/**")

        ;
        log.info("拦截器注册完成");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
